//
//  MineController.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/2.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJMineController.h"
#import "ZJMineControllerCell.h"

#import "ZJRegistrationInformationConroller.h"
#import "ZJUserChangeHeadPhoto.h"
#import "ZJReviewVC.h"
#import "ZJBankAccountVC.h"
#import "ZJGetMoneyVC.h"
#import "ZJDealVC.h"
#import "ZJBreakAccountVC.h"
#import "ZJSuccessfulEnrollmentController.h"
#import "ZJExtensionMoneyAccountVC.h"
#import "ZJBuyRoomCodeVC.h"
#import "ZJlockRoomVC.h"

#import "ZJSetVC.h"
#import "ZJAdviserVC.h"
#import "ZJExtensionVC.h"
@interface ZJMineController ()<ZJUserChangeHeadPhotoDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImg; //头像
@property (weak, nonatomic) IBOutlet UILabel *nameLabel; //名字
@property (weak, nonatomic) IBOutlet UILabel *sexLabel; //性别
@property (weak, nonatomic) IBOutlet UILabel *mineMoney; //账户余额
@property (weak, nonatomic) IBOutlet UILabel *taskMoney;

@property (weak, nonatomic) IBOutlet UILabel *hongbaoMoney; //红包余额
@property (weak, nonatomic) IBOutlet UILabel *commissionMoney; //佣金余额
@property (weak, nonatomic) IBOutlet UIView *headBgView;

@property (strong, nonatomic) NSArray * imgArr;
@property (strong, nonatomic) NSArray * titleArr;
@property (strong, nonatomic) ZJUserModel * model;
@property (weak, nonatomic) IBOutlet UIButton *ShiMingBtn;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;
@property (weak, nonatomic) IBOutlet UILabel *topTitle;
@property (weak, nonatomic) IBOutlet UIStackView *stackView;

@property (strong, nonatomic)UIAlertController *alertController;
@end

@implementation ZJMineController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
//    self.automaticallyAdjustsScrollViewInsets=NO;
    [self initData];
}

-(void)onClikModel:(ZJUserModel *)model{
    [self initData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArr = @[@"银行卡管理",@"提现",@"资金明细",@"我的报名",@"我的抢房",@"专享优惠",@"推广佣金",@"我的购房码"];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;

    
    // 设置背景图片
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.headBgView.frame];
    
    bgImgView.image = [UIImage imageNamed:kHeadImgName];
    
    [self.headBgView addSubview:bgImgView];
    
    [self.headBgView sendSubviewToBack:bgImgView];
    if (@available(iOS 11.0, *)) {
        bgImgView.sd_layout.rightSpaceToView(self.headBgView, 0).topSpaceToView(self.headBgView, -20).leftSpaceToView(self.headBgView, 0).heightIs(320);
        self.backBtn.sd_layout.leftSpaceToView(self.headBgView, 8).topSpaceToView(self.headBgView, 7).heightIs(30).widthIs(30);
        self.ShiMingBtn.sd_layout.rightSpaceToView(self.headBgView, 8).topSpaceToView(self.headBgView, 7).heightIs(30).widthIs(68);
        self.topTitle.sd_layout.centerXEqualToView(self.headImg).topSpaceToView(self.headBgView, 7).heightIs(30).widthIs(207);
        [self.headBgView setupAutoHeightWithBottomView:self.stackView bottomMargin:0];
    }
    self.headImg.layer.masksToBounds = YES;
    self.headImg.layer.cornerRadius = self.headImg.width /2;
    self.headImg.layer.borderColor = [UIColor whiteColor].CGColor;
    self.headImg.layer.borderWidth = 1.0f;
    
    [self initData];
}

-(void)initData{
    self.model = GET_USERINFO;
    
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:_model.avatar] placeholderImage:[UIImage imageNamed:KUserHeadPhoto]];
    if ([_model.real_name isEqualToString:@""]) {
         self.nameLabel.text = _model.mobile;
    }else{
    self.nameLabel.text = _model.real_name?:@"暂无名称";
    }
    self.mineMoney.text = [NSString stringWithFormat:@"%.2f",[_model.money_total floatValue]]?:@"0.00";
    self.taskMoney.text = [NSString stringWithFormat:@"%.2f",[_model.money_mission floatValue]]?:@"0.00";
    self.hongbaoMoney.text = [NSString stringWithFormat:@"%.2f",[_model.money_package floatValue]]?:@"0.00";
    self.commissionMoney.text = [NSString stringWithFormat:@"%.2f",[_model.money_commission floatValue]]?:@"0.00";
    
    self.sexLabel.textColor = [Utils colorWithHexString:@"666666"];
    if ([_model.gender isEqualToString:@"1"]) {
        self.sexLabel.text = @"先生";
    }else{
        self.sexLabel.text = @"女士";
    }
    
    if (![_model.authenticated isEqualToString:@"1"]) {
        self.ShiMingBtn.hidden = NO;
    }else{
        self.ShiMingBtn.hidden = YES;
    }
#warning 项目改动
    if ([allConstProjectid integerValue] != 7) {

        self.mineMoney.textColor = [Utils colorWithHexString:@"ffc000"];
        self.taskMoney.textColor = [Utils colorWithHexString:@"ffc000"];
        self.hongbaoMoney.textColor = [Utils colorWithHexString:@"ffc000"];
        self.commissionMoney.textColor = [Utils colorWithHexString:@"ffc000"];
    }else{
        self.mineMoney.textColor = [Utils colorWithHexString:@"e85855"];
        self.taskMoney.textColor = [Utils colorWithHexString:@"e85855"];
        self.hongbaoMoney.textColor = [Utils colorWithHexString:@"e85855"];
        self.commissionMoney.textColor = [Utils colorWithHexString:@"e85855"];
    }

    _imgArr = @[[Utils imgPath:@"wdzh_icon_yhk@3x.png"],[Utils imgPath:@"wdzh_icon_tx@3x.png"],[Utils imgPath:@"wdzh_icon_zjmx@3x.png"],[Utils imgPath:@"wdzh_icon_bmdj@3x.png"],[Utils imgPath:@"wdzg_icon_wdqf@3x.png"],[Utils imgPath:@"wdzh_icon_zxyh@3x.png"],[Utils imgPath:@"wdzh_icon_tgyj@3x.png"],[Utils imgPath:@"wdzh_icon_tgm@3x.png"]];
    
}
- (IBAction)ShiMingBtn:(UIButton *)sender {
    if ([allConstAuthenticated isEqualToString:@"0"]) {
        
        ZJRegistrationInformationConroller *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJRegistrationInformationConroller"];
        vc.isBackRoot = NO;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([allConstAuthenticated isEqualToString:@"2"]){
        
        ZJReviewVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJReviewVC"];
        vc.isBackRoot = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (IBAction)backBtn:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)userPhoto:(UITapGestureRecognizer *)sender {
    
    ZJUserChangeHeadPhoto * vc = [[ZJUserChangeHeadPhoto alloc]init];
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
//    vc.definesPresentationContext = YES;
    vc.delegate =self;
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - ***********dataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
         return _titleArr.count;
    }else if(section == 1){
        return 2;
    }else return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 10;
    }else{
        return 0;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZJMineControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineControllerCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.headImg.image = [UIImage imageWithContentsOfFile:_imgArr[indexPath.row]];
        cell.title.text = _titleArr[indexPath.row];
        
        return cell;
    }else if(indexPath.section == 1){
        switch (indexPath.row) {
            case 0:
            {
                cell.headImg.image = [UIImage imageWithContentsOfFile:[Utils imgPath:@"wdzh_icon_gw@3x"]];
                cell.title.text = @"我的顾问";
            }
                break;
            case 1:
            {
                cell.headImg.image = [UIImage imageWithContentsOfFile:[Utils imgPath:@"wdzh_icon_set@3x"]];
                cell.title.text = @"设置";
            }
                break;
                
            default:
                break;
        }
        return cell;
    }else return nil;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
    
    switch (indexPath.row) {
        case 0:
        {
            if ([self.model.authenticated isEqualToString:@"0"]) {
                [HUD showMsg:@"请先完成实名认证"];
                ZJRegistrationInformationConroller *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJRegistrationInformationConroller"];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if ([self.model.authenticated isEqualToString:@"2"]){
                
                ZJReviewVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJReviewVC"];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if ([self.model.authenticated isEqualToString:@"1"]){
            
                ZJBankAccountVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJBankAccountVC"];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            
        }
            break;
            
        case 1:
        {
            
            if ([self.model.bank isEqualToString:@""] || [self.model.account isEqualToString:@""]) {
                self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先添加银行卡" preferredStyle:UIAlertControllerStyleAlert];
                
                [self presentViewController:_alertController animated:YES completion:nil];
                
                [self performSelector:@selector(hide) withObject:self afterDelay:2.5];
                
            }else{
                ZJGetMoneyVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJGetMoneyVC"];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
            
        case 2:
        {
            ZJDealVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJDealVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 3:
        {
            ZJUserModel * model = GET_USERINFO;
            if ([model.is_sincere integerValue] != 1) {
                self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请完成提前报名" preferredStyle:UIAlertControllerStyleAlert];
                
                [self presentViewController:_alertController animated:YES completion:nil];
                
                [self performSelector:@selector(hide) withObject:self afterDelay:2.5];
                
            }else{
                
                if ([model.is_sincere isEqualToString:@"1"]) {
                    
                    if ([model.deposit_return integerValue] !=1) {
                        
                        if ([model.deposit_can_return integerValue] ==1) {
                            ZJSuccessfulEnrollmentController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSuccessfulEnrollmentController"];
                            vc.deposit_return_type = @"退诚意金";
                            [self.navigationController pushViewController:vc animated:YES];
                            
                        }else{
                            ZJSuccessfulEnrollmentController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSuccessfulEnrollmentController"];
                            [self.navigationController pushViewController:vc animated:YES];
                        }
                        
                    }else{
                        
                        ZJSuccessfulEnrollmentController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSuccessfulEnrollmentController"];
                        vc.deposit_return_type = @"已退诚意金";
                        [self.navigationController pushViewController:vc animated:YES];
                        
                    }
                    
                }else{
                    
                    ZJSuccessfulEnrollmentController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSuccessfulEnrollmentController"];
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }

                
            }
        }
            break;
            
        case 5:
        {
            ZJBreakAccountVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJBreakAccountVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 6:
        {
            if ([self.model.is_promoter integerValue] != 1) {
                self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先成为推广员" preferredStyle:UIAlertControllerStyleAlert];
                
                [self presentViewController:_alertController animated:YES completion:nil];
                
                [self performSelector:@selector(hide) withObject:self afterDelay:2.5];
                
            }else{
            ZJExtensionMoneyAccountVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJExtensionMoneyAccountVC"];
            [self.navigationController pushViewController:vc animated:YES];
            }
        }
            break;
            
        case 4:
        {
            if ([self.model.has_buy_house integerValue] == 1) {
                ZJlockRoomVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJlockRoomVC"];
                vc.userModel = self.model;
                vc.strTitle = @"我的抢房";
                ZJRoomModel * rmmodel = [ZJRoomModel mj_objectWithKeyValues:self.model.house_buy];
                vc.model = rmmodel;
                vc.isBack = YES;
                [self.navigationController pushViewController:vc animated:YES];
                
            }else{
                self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先完成在线购房" preferredStyle:UIAlertControllerStyleAlert];
                
                [self presentViewController:_alertController animated:YES completion:nil];
                
                [self performSelector:@selector(hide) withObject:self afterDelay:2.5];
            }
        }
            break;
            
        case 7:
        {
            if ([self.model.is_sincere integerValue] != 1) {
                
                self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先完成提前报名" preferredStyle:UIAlertControllerStyleAlert];
                
                [self presentViewController:_alertController animated:YES completion:nil];
                
                [self performSelector:@selector(hide) withObject:self afterDelay:2.5];
                
            }else{
                
                ZJBuyRoomCodeVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJBuyRoomCodeVC"];
                vc.codeStr = self.model.shop_code;
                [self.navigationController pushViewController:vc animated:YES];
                
            }

        }
            break;
            
        default:
            break;
    }
        
    }else if (indexPath.section == 1){
        switch (indexPath.row) {
            case 0:
            {
                NSDictionary * dict = self.model.consultant;
                if (dict[@"mobile"]) {
                    ZJAdviserVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJAdviserVC"];
                    vc.dict = dict;
                    [self.navigationController pushViewController:vc animated:YES];
                }else{
                    ZJExtensionVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJExtensionVC"];
//                    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
                    vc.type = NO;
                    [self presentViewController:vc animated:YES completion:nil];
                }
            }
                break;
            case 1:
            {
                ZJSetVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSetVC"];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            default:
                break;
        }
    }
}
#pragma mark - 补全分隔线左侧缺失
- (void)viewDidLayoutSubviews {
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

-(void)hide{
    
    [self.alertController dismissViewControllerAnimated:YES completion:nil];
    
}
@end
